if select(2, UnitClass("player")) ~= "SHAMAN" then return end
local beTor = CreateFrame("Frame", nil, UIParent)

beTor:RegisterEvent"PLAYER_LOGIN"
beTor:RegisterEvent"COMBAT_LOG_EVENT_UNFILTERED"
beTor:RegisterEvent"PLAYER_TOTEM_UPDATE"
beTor:RegisterEvent"UNIT_AURA"
beTor:RegisterEvent"PARTY_MEMBERS_CHANGED"

--
-- you shouldnt change anything above

    local beTorSettingsShowMissing = true -- if this is true totems which are MISSING are shown, if false totem which are NOT MISSING are shown
    local beTorSettingsAbbreviate = true -- shorten those totem names
    
    function beTor:init()
        beTorText = self:CreateFontString(nil, "OVERLAY")
       	beTorText:SetPoint("LEFT", self) -- text orientation
       	beTorText:SetTextColor(1,1,1) -- text color for default (non class) text
       	beTorText:SetFont("Fonts\\ARIALN.ttf", 12, "OUTLINE") -- config font things here
    
        self:SetPoint("LEFT", UIParent, "LEFT", 20, 0) -- frame orientation 
    end

-- you shouldnt change anything below 
--

local totems = {    
    ["Fire Resistance Totem IV"] = "Fire Resistance",
    ["Frost Resistance Totem IV"] = "Frost Resistance",
    ["Grace of Air Totem III"] = "Grace of Air",
    ["Grounding Totem"] = "Grounding Totem Effect",
    ["Healing Stream Totem VI"] = "Healing Stream",
    ["Mana Spring Totem V"] = "Mana Spring",
    ["Nature Resistance Totem IV"] = "Nature Resistance",
    ["Stoneskin Totem VIII"] = "Stoneskin",
    ["Strength of Earth Totem VI"] = "Strength of Earth",
    ["Tranquil Air Totem"] = "Tranquil Air",
    ["Windwall Totem IV"] = "Windwall",
    ["Wrath of Air Totem"]  = "Wrath of Air Totem",
}

local active = {
    [1]="",
    [2]="",
    [3]="",
    [4]=""
}

local players = {}
       
local hexColors = {}
for k, v in pairs(RAID_CLASS_COLORS) do
    hexColors[k] = ("|cff%02x%02x%02x"):format(v.r * 255, v.g * 255, v.b * 255)
end
   
function beTor:abbr(name)
  if (string.find(name, "[%s%-]")) then
   name = string.gsub(name, "(%a)[%l%p]*[%s%-]*", "%1");
  end
  return name;
end

function beTor:UpdateDisplay()
    local allText = {}
    local displayText = ""
    local i = 1
    
    for playerName, auraArray in pairs(players) do -- iterate over player array
        local auraText = "" -- for each player reset the aura text
        local bShow = false -- and also the boolean to show
        for auraId, auraName in pairs(auraArray) do --
            if auraName == "" then
                local name = active[auraId]
                if name ~= "" then
                    if beTorSettingsAbbreviate then
                        name = beTor:abbr(name)
                    end
                    auraText = auraText .. name .. " "
                    bShow = true
                end
            end
        end
        if bShow and auraText ~= "" then
            allText[i] = playerName ..  ": " .. auraText
        end
        i=i+1
    end 
    
    for k, v in pairs(allText) do
        if allText[k] ~= "" and allText[k] ~= nil then
            displayText = displayText .. "\n" .. allText[k]
        end
    end
    
    beTorText:SetText(displayText)
    self:SetHeight(beTorText:GetHeight())
	self:SetWidth(beTorText:GetWidth())
end

-- cl event
beTor:SetScript("OnEvent", function(self, event, timeStamp, eventType, sourceGUID, sourceName, sourceFlags, destGUID, destName, destFlags) 
    if event=="PARTY_MEMBERS_CHANGED" then
        -- reset the array on partychange
        players = {}
    elseif event=="PLAYER_TOTEM_UPDATE"  then
        -- activate a totem
        local _, name, _, duration = GetTotemInfo(arg1)
        local slot = arg1
        if name ~= 0 or name ~= nil then
            if totems[name] then
                active[slot] = totems[name]
            else
                active[slot] = ""
            end
        end
    elseif event=="UNIT_AURA"  then
        -- update auras (totem buff check)
        if UnitInParty(arg1) then
             local auras = {
                [1]="",
                [2]="",
                [3]="",
                [4]=""
             }
             for k, v in pairs(active) do
                if active[k] ~= "" then
                    if beTorSettingsShowMissing then
                        auras[k] = ""
                        if beTor:IsBuffActive(active[k],arg1) then
                            auras[k] = active[k]
                        end
                    else
                        auras[k] = active[k]
                        if beTor:IsBuffActive(active[k],arg1) then
                            auras[k] = ""
                        end
                    end
                end
             end   
             local _, class = UnitClass(arg1)
             local myName = hexColors[class] .. UnitName(arg1) .. "|r"
             players[myName] = auras
        end
        beTor:UpdateDisplay()
    elseif event == "COMBAT_LOG_EVENT_UNFILTERED" then       
        -- totem death check
        if eventType == "UNIT_DIED" then
            for k, v in pairs(active) do
                if active[k] == destname then
                    active[k] = ""
                end
            end
        end
    elseif event == "PLAYER_LOGIN" then
        -- init addon
        beTor:init()    
    end 
end)

function beTor:IsBuffActive(buffName,unit)
    local i = 1
	local buff = UnitBuff(unit, i)
    while buff do
		if buff == buffName then
            return true
		end
		i = i + 1;
		buff = UnitBuff(unit, i);
	end
	
    return false
end

-- following only development functions

function beTor:DebugMessage(message) -- write normal messages
    -- default ChatFrame1
    ChatFrame4:AddMessage("|cff0033CCbe|r|cff888888Tor|r: " .. message)
end

function beTor:DisplayArray(array)
    for k, v in pairs(array) do
        beTor:DebugMessage("__" .. k)
        for key, value in pairs(v) do
             beTor:DebugMessage("_____" .. key.. " " ..value)
        end
    end
end